let submit = document.getElementById('submit');
function delay(ms){
    let start = new Date().getTime();
    while(start + ms > new Date().getTime()){}
}  
submit.onclick = function (e){ 
    e ? e.preventDefault() : window.e.returnValue = false; 
    let auto = document.getElementById('auto');
    let user = document.getElementById('user');
    let pwd = document.getElementById('pwd');
    let msg = document.getElementById('msg');
    let tip = document.getElementById('tip');
    if(user.value.trim() == ''){
      user.focus();
    }else if(pwd.value == ''){
      pwd.focus();
    }else{
      tip.className = 'normal'
      tip.innerText = "正在登录...";
      tip.style.top = "20px";
      let xmlhttp;
      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.open("POST","ajax/check.php?type=2",false);
      xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      xmlhttp.send(`user=${user.value.trim()}&pwd=${pwd.value}&auto=${auto.checked}`);
      let {ishave,islogin,reurl} = JSON.parse(xmlhttp.response);
      if(!ishave){
        setTimeout(function() {
            setTimeout(function(){
                tip.style.top = "-58px";
            },500)
            tip.className = "faild"
            tip.innerText = "登录失败";
            msg.innerHTML = '<div class="err-net">账号或密码错误</div>';
        }, 1000);
      }else{
          if(islogin){
              setTimeout(function() {
                setTimeout(function(){
                    tip.style.top = "-58px";
                },500)
                tip.className = "faild"
                tip.innerText = "登录失败";
                msg.innerHTML = '<div class="err-net">您已经登录了</div>';
            }, 1000);
          }else{
              
              setTimeout(function() {
                  tip.className = "success"
                  tip.innerText = "登录成功"
                  setTimeout(function(){
                      window.location.href = reurl
                  },500)
              }, 1000);
          }
      }
    }
    
    

};